/****************************************************************************

 XDV Game Engine
 Copyright 2013 - Xavier Durand-Hollis, David Ward, Vitor Makoto Matayoshi
 
 XDVGameObject.cpp - Created May 30, 2013 by Xavier

 ****************************************************************************/

#include "XDVGameObject.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

XDVGameObject::XDVGameObject()
{
	m_name = "GameObject";
	m_tag = "Default"; // TODO: Tag system l0l
	m_activeSelf = true;
}
XDVGameObject::XDVGameObject(std::string name) : m_name(name)
{
	m_tag = "Default";
	m_activeSelf = true;
}

XDVGameObject::~XDVGameObject()
{
	for(unsigned i = 0; i < m_components.size(); ++i){
		delete m_components[i];
	}
}

void XDVGameObject::Awake()
{
	// Called before Start
}

void XDVGameObject::Start()
{
	// Called before first Update
}

void XDVGameObject::FixedUpdate(float deltaTime)
{
	// Called every fixed deltaTime. The time passed as a parameter should be constant.
}

void XDVGameObject::Update(float deltaTime)
{
	// Called every frame. deltaTime may vary depending on frame rate.
}

void XDVGameObject::LateUpdate(float deltaTime)
{
	// Called every frame after Update and animation calculations
}



XDVComponent* XDVGameObject::GetComponent(std::string type)
{
	std::transform(type.begin(), type.end(), type.begin(), ::tolower);
	std::cout << type << std::endl;
	for(unsigned i = 0; i < m_components.size(); ++i){
		if(m_components[i]->GetName().compare(type) == 0){

			return m_components[i];
		}
	}
	std::cout << "Could not find component " << type << std::endl;
	return NULL;
}

std::vector<XDVComponent*> XDVGameObject::GetComponents(std::string type)
{
	std::vector<XDVComponent*> components;
	for(unsigned i = 0; i < m_components.size(); ++i){
		if(m_components[i]->GetName().compare(type) == 0){
			components.push_back(m_components[i]);
		}
	}
	return components;
}

XDVComponent* XDVGameObject::AddComponent(XDVComponent& component){

	component.SetGameObject(*this);
	m_components.push_back(&component);
	cout << "Added component." << endl;
}

void XDVGameObject::SetActiveSelf(bool b)
{
	for(unsigned i = 0; i < m_components.size(); ++i){
		m_components[i]->SetActive(b);
	}
	m_activeSelf = b;
}


